package com.weixin.project.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Map;
import java.util.Set;

@RestController
public class URLController {

    @Autowired
    private ApplicationContext context;

    /**
     * 查看已注册的所有接口
     * @return 已注册的所有接口
     */
    @GetMapping("/mappings")
    public String getMappings() {
        RequestMappingHandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
        StringBuilder result = new StringBuilder();
        for (RequestMappingInfo info : handlerMethods.keySet()) {
            Set<String> patterns = info.getPatternsCondition().getPatterns();
            for (String pattern : patterns) {
                result.append(pattern).append("\n");
            }
        }
        return result.toString();
    }

}
